package ncu.me.musicplayer.adapter;

import android.graphics.Color;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import ncu.me.musicplayer.entity.MusicInfo;
import ncu.me.musicplayer.R;


public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.MyHolder> {
    private int mIndex = -1;
    private List<MusicInfo> mList;

    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    private SparseArray<OnItemClickListener> mInnerListeners= new SparseArray<>();

    public void setIndex(int index) {
        this.mIndex = index;
    }

    public List<MusicInfo> getList() {
        return mList;
    }

    public void setList(List<MusicInfo> list) {
        this.mList = list;
    }
    public MusicInfo get(int index){
        if (mList == null || mList.size() == 0){
            return null;
        }
        return mList.get(index);
    }
    public MusicInfo get(){
        if (mList == null || mList.size() == 0){
            return null;
        }
        return mList.get(mIndex);
    }
    public MusicInfo getLast(){
        if (mList == null || mList.size() == 0){
            return null;
        }
        return mList.get(mList.size() - 1);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.music_item, parent, false);

        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        // listeners
        if (mOnItemClickListener != null){
            holder.itemView.setOnClickListener(v -> mOnItemClickListener.onClick(holder.itemView,position));
        }
        if (mOnItemLongClickListener != null){
            holder.itemView.setOnLongClickListener(v->mOnItemLongClickListener.onLongClick(holder.itemView,position));
        }
        for (int i = 0; i < mInnerListeners.size(); i++) {
            int id = mInnerListeners.keyAt(i);
            View view = holder.itemView.findViewById(id);
            OnItemClickListener listener = mInnerListeners.valueAt(i);
            if (view != null && listener!= null){
                view.setOnClickListener(v -> listener.onClick(holder.itemView,position));
            }
        }
        //views
        holder.tvName.setText( mList.get(position).getName());
        holder.tvSongName.setText( mList.get(position).getSongName());
        if (position==mIndex){
            holder.itemView.setBackgroundColor(Color.parseColor("#cccccc"));
        }else{
            holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"));
        }
    }

    @Override
    public int getItemCount() {
        if (mList != null){
            return mList.size();
        }
        return 0;
    }

    public interface OnItemClickListener{
        void onClick(View view, int pos);
    }
    public interface OnItemLongClickListener{
        boolean onLongClick(View view, int pos);
    }

    class MyHolder extends RecyclerView.ViewHolder{

        TextView tvName;
        TextView tvSongName;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.item_name);
            tvSongName = itemView.findViewById(R.id.item_song_name);
        }
    }
}
